package com.fairytask.ui
{
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.GoodSet;
	import com.fairytask.ui.base.BaseUI;
	import com.fairytask.ui.item.BagItemBak;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 *其他玩家装备 
	 * @author pangj
	 * 
	 */
	public class OtherEquip extends BaseUI
	{
		public var modelBg     :Clip;
		
		public var eq_head_bg  :MovieClip;		//头
		public var eq_line_bg  :MovieClip;		//链
		public var eq_arm_bg   :MovieClip;		//武
		public var eq_chest_bg :MovieClip;		//衣
		public var eq_foot_bg  :MovieClip;		//鞋
		public var eq_ring_bg  :MovieClip;		//戒
		
		public var infoBG     :Window;
		public var nameLab    :Label;
		public var levelLab   :Label;
		
		private var personEquip :Sprite = new Sprite();//装备容器
		private var modelSpace  :Sprite = new Sprite();//人物模型容器
		private var weaponSpace :Sprite = new Sprite();//装备模型容器
		private var weaponEffect:Sprite = new Sprite();//装备效果
		
		private var _modelMc   :MovieClip;//人物模型
		private var _weaponMc  :MovieClip;//装备模型
		private var _effectMode:MovieClip;//装备模型
		
		private var _equip     :Array = [];//装备
		private var _eqObj     :Array = [];//装备对象
			
		//private var _curObj    :Point;
		//private var _curPos    :int;
		
		public function OtherEquip()
		{
			super();
			initEquip();
		}
		private function initEquip():void
		{
			_equip = new Array("",eq_head_bg,
				eq_arm_bg,
				eq_foot_bg,
				eq_line_bg,
				eq_chest_bg,
				eq_ring_bg);
			
			personEquip.x = 60;
			personEquip.y = 105;
			addChild(personEquip);
			personEquip.addChild(modelSpace);
			personEquip.addChild(weaponSpace);
			personEquip.addChild(weaponEffect);
			personEquip.mouseEnabled = false;
			personEquip.mouseChildren= false;
			
			for (var i:int=1;i<_equip.length;i++)
			{
				_equip[i].pos = i;
				_equip[i].mouseChildren=false;
				_equip[i].addEventListener(MouseEvent.MOUSE_OVER,onOver);
				_equip[i].addEventListener(MouseEvent.MOUSE_OUT,onOut);
				var equip:BagItemBak = new BagItemBak();
				equip.x	=	-2;
				equip.y	=	-2;
				_equip[i].addChild(equip);
				_equip[i].data = null;
				_equip[i].equip = equip;
				_equip[i].ObjKey = "";
			}
		}
		
		/**
		 * 显示装备tips 
		 * @param evt
		 * 
		 */
		private function onOver(evt:MouseEvent):void
		{
			if(_equip[evt.currentTarget.pos].data!=null)
			{
				var viewObj : ViewObject = _eqObj[evt.currentTarget.pos];
				var configID:int=viewObj.GetProperties("ConfigID");
				TipsManage.ShowAllItemsTips(int(StaticIni.getIniVar(String(configID),"ItemType")),configID,viewObj);
				
			}
		}
		
		/**
		 * 隐藏tips 
		 * @param evt
		 * 
		 */
		private function onOut(evt:MouseEvent):void
		{
			ToolTips.hide();
		}
		
		/**
		 *添加装备 
		 * @param pos
		 * @param val
		 * 
		 */
		public function addEquip(pos:int,val:ViewObject):void
		{
			_eqObj[pos] = val;
			if(val.GetPropertiesEx("ConfigID")=="")
			{
				return;
			}
			if(_equip[pos].data == null)
			{
				_equip[pos].ObjKey = val.ObjKey;
				_equip[pos].data = val;
			}
			var cfgID:int = val.GetPropertiesEx("ConfigID");
			changeImg(cfgID,pos);
		}
		/**
		 * 删除装备
		 * @param pos
		 * 
		 */
		public function clearEquip(pos:int):void
		{	
			if(_equip[pos].data!=null)
			{
				//				(_equip[pos]as MovieClip).removeChild(_equip[pos].equip);
				(_equip[pos] as MovieClip).removeChild(_equip[pos].frame);
				_equip[pos].equip.clearPhoto();
				_equip[pos].data=null;
				_equip[pos].ObjKey = "";
				//				_equip[pos].clip=null;
				_equip[pos].frame=null;
				
				_equip[pos].frameColor=null;
			}
			
		}
		/**
		 * 更改图标 
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */
		private function changeImg(val:int,pos:int):void
		{
			//物品框颜色设置
			if(null != _equip[pos].frame)
			{
				_equip[pos].removeChild(_equip[pos].frame);
				_equip[pos].frame = null;
				_equip[pos].frameColor = null;
			}
			var frameData:Array	= GoodSet.getFrameData(val);
			var goodFrame :Shape = frameData[0];
			var frameColor:int   = frameData[1];
			_equip[pos].frame = goodFrame;
			_equip[pos].frameColor = frameColor;
			goodFrame.x = -1;
			goodFrame.y = -1;
			_equip[pos].addChild(goodFrame);
			//物品图标
			_equip[pos].equip.source = ResConfig.iconPath + StaticIni.getIniVar(val,"Icon") + ResConfig.iconImg;
		}
		/**
		 * 设置人物模型 
		 * @param val
		 * 
		 */
		public function set roleModel(val:String):void
		{
			//SwfLoader.StartLoad(ResConfig.npcResPath+val+"_model.swf",_modelMc,false,onLoadModel);
		}
		/**
		 * 加载人物模型 
		 * @param val
		 * @param pList
		 * 
		 */
		private function onLoadModel(val:DisplayObject,pList:Array):void
		{
			_modelMc	=	val as MovieClip;
			if(_modelMc)
			{
				while(modelSpace.numChildren>0)
				{
					modelSpace.removeChildAt(0);
				}
				_modelMc.x	=	82;
				_modelMc.y	=	76;
				modelSpace.addChild(_modelMc);
				playRes();
			}
		}
		/**
		 *显示姓名与等级 
		 * @param name
		 * @param level
		 * 
		 */
		public function setModelInfo(name:String,level:String):void
		{
			level = "LV." + level;
			nameLab.htmlStrByBorder  = name;
			levelLab.htmlStrByBorder = level;
			nameLab.x = int(infoBG.width - nameLab.width)/2-5;
			levelLab.x= int(infoBG.width - levelLab.width)/2-5;
		}
		/**
		 * 资源同步 
		 * 
		 */
		private function playRes():void
		{
			//同步
			if(null != _weaponMc)
			{
				_weaponMc.gotoAndPlay(0);
			}
			if(null != _modelMc)
			{
				_modelMc.gotoAndPlay(0);
			}
			if(null != _effectMode)
			{
				_effectMode.gotoAndPlay(0);
			}
		}
		/**
		 * 清除所有内容
		 * 
		 */
		public function clearAll():void
		{
			//清除人物模型
			while(modelSpace.numChildren>0)
			{
				modelSpace.removeChildAt(0);
			}
			//清除装备模型
			while(weaponSpace.numChildren>0)
			{
				weaponSpace.removeChildAt(0);
			}
			//清除装备特效
			while(weaponEffect.numChildren>0)
			{
				weaponEffect.removeChildAt(0);
			}
			//清除装备显示
			for(var i:int=1;i<_equip.length;i++)
			{
				clearEquip(i);
			}
			_eqObj = [];
			nameLab.htmlStrByBorder = "";
			levelLab.htmlStrByBorder= "";
		}
		/**
		 *其他玩家装备模型 
		 * @param val
		 * @param sex
		 * 
		 */
		public function WeaponModel(val:int,sex:int):void
		{
			if(sex==2)
			{
				//SwfLoader.StartLoad(ResConfig.weaponResPath + val + "_2.swf",_weaponMc,false,onWeaponLoad);
			}
			else if(sex==1)
			{
				//SwfLoader.StartLoad(ResConfig.weaponResPath + val + "_1.swf",_weaponMc,false,onWeaponLoad);	
			}
			else
			{
				while(weaponSpace.numChildren>0)
				{
					weaponSpace.removeChildAt(0);
				}
			}
		}
		/**
		 * 加载装备模型 
		 * @param val
		 * @param pList
		 * 
		 */
		private function onWeaponLoad(val:DisplayObject,pList:Array):void
		{
			if(val)
			{
				while(weaponSpace.numChildren>0)
				{
					weaponSpace.removeChildAt(0);
				}
				_weaponMc = val as MovieClip;
				weaponSpace.addChild(_weaponMc);
				_weaponMc.x = 82;
				_weaponMc.y = 76;
				//同步
				_weaponMc.mouseEnabled = false;
				_weaponMc.mouseChildren= false;
				playRes();
			}
		}
		
	}
}